// pages/addAddr/index.js
const app = getApp()
Page({
    data: {
        url: app.globalData.url,
        name: undefined,
        address: undefined,
        phone: undefined,
        defaultAdd: undefined,
        addr: undefined
    },
    getName(e) {

        if (e.detail.value) {
            let addr = this.data.addr
            if (addr) {
                addr.name = e.detail.value
            }
            this.setData({ name: e.detail.value, addr })
        }
    },
    getAddress(e) {
        if (e.detail.value) {
            let addr = this.data.addr
            if (addr) {
                addr.address = e.detail.value
            }
            this.setData({ address: e.detail.value, addr })
        }
    },
    getPhone(e) {
        if (e.detail.value) {
            let addr = this.data.addr
            if (addr) {
                addr.phone = e.detail.value
            }
            this.setData({ phone: e.detail.value, addr })
        }
    },
    getDefault(e) {
        let addr = this.data.addr
        if (addr) {
            addr.defaultAdd = e.detail.value
        }
        this.setData({ defaultAdd: e.detail.value, addr })
    },
    add() {
        const that = this
        wx.showLoading({
            title: '新增中...',
        })
        wx.request({
            url: that.data.url + "/address/add",
            method: "POST",
            data: JSON.stringify({uid: that.data.user.id,
                name: that.data.name,
                address: that.data.address,
                phone: that.data.phone,
                defaultAdd: that.data.defaultAdd}),
            success(res) {
                if (res.data.code === 1) {
                    wx.hideLoading({
                        success: (res) => {
                            wx.showToast({
                                title: '添加成功',
                                success() {
                                    wx.navigateBack({
                                        delta: 0
                                    })
                                }
                            })
                        },
                    })

                }
            }
        })
    },
    edit() {
        const that = this
        wx.showLoading({
            title: '修改中...',
        })
        wx.request({
            url: that.data.url + "/address/edit",
            method: "POST",
            data: JSON.stringify(that.data.addr),
            success(res) {
                if (res.data.code == 1) {
                    wx.hideLoading({
                        success: (res) => {
                            wx.showToast({
                                title: '修改成功',
                                success(res) {
                                    wx.navigateBack({
                                        delta: 0,
                                    })
                                }
                            })
                        },
                    })
                }
            }
        })
    },
    remove(e) {
        const that = this
        wx.request({
            url: that.data.url + "/address/remove?id=" + e.currentTarget.dataset.id,
            success(res) {
                if (res.data.code == 1) {
                    wx.navigateBack({
                        delta: 0,
                    })
                }
            }
        })
    },
    onShow() {
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
        } else {
            // 发送请求登录
        }
    },
    onLoad(options) {
        if (options.address) {
            this.setData({ addr: JSON.parse(options.address) })
            wx.setNavigationBarTitle({
                title: '修改收货地址',
            })
        }
    }
})